<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/9/12
 * Time: 上午10:14
 */

namespace app\admin\logic;


use extend\Functions;
use extend\STATUS_CODE;

class Club extends Base
{
    /**
     * @purpose 修改俱乐部状态
     * @param int $status
     * @param string $uuid
     * @return array
     */
    public static function changeStatus (int $status, string $uuid): array
    {
        if (!in_array($status, [1,2])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '错误的状态!'
            );
        }
        if (!$uuid) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误'
            );
        }
        $model      = new \app\model\Club();
        $result     = $model
            ->isUpdate(true)
            ->save(['status' => $status], ['uuid' => $uuid]);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '操作成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::UPDATE_FAIL,
            '操作失败!'
        );
    }
    /**
     * @purpose 获取列表
     * @param int $page
     * @param int $size
     * @param array $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function items (int $page = 1, int $size = 50, array $where = []): array
    {
        $model          = new \app\model\Club();
        $total          = $model
            ->where($where)
            ->count(1);
        $list           = $model
            ->where($where)
            ->where('is_child', 1)
            ->field('uuid,mobile,email,name,logo,status')
            ->page($page,$size)
            ->select();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'total' => $total,
                'list'  => $list
            ]
        );
    }
    /**
     * @purpose 创建俱乐部
     * @param array $data
     * @return array
     */
    public static function create(array $data): array
    {
        if (!$data['logo']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少俱乐部LOGO...!'
            );
        }
        if (!$data['name']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少俱乐部名称...!'
            );
        }
        if (!$data['mobile']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少手机号码...!'
            );
        }
        if (!Functions::check_phone_number($data['mobile'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '手机号码格式错误...!'
            );
        }
        if (!$data['email']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少邮箱...!'
            );
        }
        if (!Functions::check_email($data['email'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '邮箱格式错误...!'
            );
        }
        if (!$data['password']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少密码...!'
            );
        }
        if (!preg_match('/^[0-9a-zA-Z]{8,}$/', strtolower($data['password']))) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '密码格式错误，请输入8位以上数字与字母的组合，不区分大小写!'
            );
        }
        $model = new \app\model\Club();
        if ($model->where('name', trim($data['name']))->count(1) > 0) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '俱乐部名称已存在...!'
            );
        }
        if ($model->where('mobile', trim($data['mobile']))->count(1) > 0) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '手机号码已存在...!'
            );
        }
        if ($model->where('email', trim($data['email']))->count(1) > 0) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '邮箱已存在...!'
            );
        }
        $_data           = Functions::password(strtolower(trim($data['password'])));
        $_data['mobile'] = trim($data['mobile']);
        $_data['email']  = trim($data['email']);
        $_data['logo']   = trim($data['logo']);
        $_data['name']   = trim($data['name']);
        $_data['uuid']   = strtolower(Functions::getUUID());
        RE_UUID:
            $_data['uuid']   = strtolower(Functions::getUUID());
        if ($model->where('uuid', $_data['uuid'])->count(1) > 0) {
            goto RE_UUID;
        }
        $result     = $model
            ->isUpdate(false)
            ->data($_data)
            ->save();
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '添加成功'
            );
        }
        return Functions::result(
            STATUS_CODE::CREATE_FAIL,
            '添加失败!'
        );
    }
}